package com.joycessh.action;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

import com.joycessh.dao.ITopicDao;
import com.joycessh.dao.impl.TopicDaoImpl;
import com.joycessh.vo.TopicVO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CreateTopicAction extends ActionSupport {

	/**
	 * @author joycessh
	 */
	private static final long serialVersionUID = 1L;
	
	private TopicVO topic;
	private ITopicDao tpDao;
	public TopicVO getTopic() {
		return topic;
	}
	public void setTopic(TopicVO topic) {
		this.topic = topic;
	}
	@Override
	public String execute() {
		// TODO Auto-generated method stub
		tpDao = new TopicDaoImpl();
		
		ActionContext actionContext = ActionContext.getContext();
	    @SuppressWarnings("unchecked")
		Map<String, String> session = actionContext.getSession();
		String author = session.get("username");
		
		Timestamp createDate = new Timestamp(System.currentTimeMillis());
		
		try {
			if (tpDao.createTopic(topic.getTitle(), topic.getContent(), author, createDate)) {
				return "success";
			} else {
				return "input";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "input";
		}
				
	}
		
}
